/*
 * @Author: lin_zizhuo 2297304135@qq.com
 * @Date: 2023-11-23 09:34:25
 * @LastEditors: lin_zizhuo 2297304135@qq.com
 * @LastEditTime: 2023-12-01 07:12:38
 * @FilePath: /signal_text/process.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: lin_zizhuo 2297304135@qq.com
 * @Date: 2023-11-23 09:33:13
 * @LastEditors: lin_zizhuo 2297304135@qq.com
 * @LastEditTime: 2023-11-23 09:34:25
 * @FilePath: /signal_text/process.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <signal.h>
using namespace std;

void printPending()
{
    sigset_t pending;
    sigpending(&pending);
    for (int i = 31; i > 0; --i)
        cout << sigismember(&pending, i);
    cout << endl;
}

// signal可以用来修改信号的处理动作
void myhandler(int signum)
{
    sigset_t block, bset;
    sigemptyset(&block);
    sigprocmask(SIG_SETMASK, &block, &bset);
    // kill(getpid(), 2);
    cout << "I get a signal\n";
    int cnt = 10;
    while (cnt)
    {

        printPending();
        cout << "the signal: " << signum << " cnt : " << cnt << endl;
        cnt--;
        sleep(1);
    }
    // exit(1);
}

int main()
{
    // for(int i = 0; i <= 31; ++i)
    // signal(i, myhandler);
    struct sigaction set, oset;
    memset(&set, 0, sizeof set);
    // memset(&set, 0, sizeof set);
    set.sa_handler = myhandler;
    sigaction(2, &set, nullptr);
    while (true)
    {
        cout << "I'm a crazy process, my pid is : " << getpid() << " !\n";
        sleep(1);
    }
    // 死循环，该如何退出？
    // int a = 10;
    // a /= 0;

    return 0;
}
